//
//  ACWChargeReservationVC.m
//  aichewang
//
//  Created by ucs on 16/2/26.
//  Copyright © 2016年 aichewang. All rights reserved.
//

#import "ACWChargeReservationVC.h"
#import "RLPullDownMenu.h"
#import "ACWMapModeVC.h"
#import "ACWChargeConfirmVC.h"

#define beijin  @[@"line1", @"line2", @"line1", @"line2"]
#define shanghai @[@"line1A", @"line1B", @"line1A", @"line1B", @"line1C", @"line1D", @"line1E", @"line1F", @"line1G", @"line1H", @"line1I", @"line1J"]
#define guangzhou  @[@"line2A", @"line2B", @"line2A", @"line2B", @"line2C", @"line2D", @"line2E", @"line2F"]
#define hangzhou  @[@"line2A", @"line2B", @"line2A", @"line2B", @"line2C", @"line2D", @"line2E", @"line2F"]

@interface ACWChargeReservationVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDelegate, UITableViewDataSource, RLPullDownMenuDelegate>
{
    UICollectionView *_districtCollectionView;
    NSInteger _selectedIndex;
}
@property (weak, nonatomic) IBOutlet UILabel *distructLable;
@property (weak, nonatomic) IBOutlet RLPullDownMenu *cityMenu;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *districtNames;
@end

static NSString *ID = @"Cell";
@implementation ACWChargeReservationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"预约充电";
    _selectedIndex = INT_MAX;
    _cityMenu.delegate = self;
    _cityMenu.itemsArray = @[@"选择城市", @"北京", @"上海", @"广州", @"杭州"];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize =CGSizeMake((screenW - 8) / 3, 30);
    flowLayout.minimumLineSpacing = 2;
    flowLayout.minimumInteritemSpacing = 2;
    flowLayout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _districtCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 84 + 30 + 10, screenW, 70) collectionViewLayout:flowLayout];
    _districtCollectionView.backgroundColor = [UIColor lightGrayColor];
    _districtCollectionView.delegate = self;
    _districtCollectionView.dataSource = self;
    [self.view addSubview:_districtCollectionView];
    
    // 注册重用cell
    [_districtCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
}

- (IBAction)mapMode {
    ACWMapModeVC *mapModeVC = [ACWMapModeVC new];
    [self pushNavControllerWithoutBottomBar:mapModeVC];
}



#pragma mark ------------------------------------------------------------
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] init];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"**物业 **路**号 车位%ld", indexPath.row];
    return cell;
}

#pragma mark ------------------------------------------------------------
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ACWChargeConfirmVC *chargeConfirmVC = [ACWChargeConfirmVC new];
    [self pushNavControllerWithoutBottomBar:chargeConfirmVC];
}

#pragma mark ------------------------------------------------------------
#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _districtNames.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    if (_selectedIndex == indexPath.item) {
        cell.backgroundColor = [UIColor blueColor];
    }else {
        cell.backgroundColor  = [UIColor whiteColor];
    }
    
    UILabel *districtLable = [[UILabel alloc] initWithFrame:cell.bounds];
    districtLable.textAlignment = NSTextAlignmentCenter;
    districtLable.text = _districtNames[indexPath.item];
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    [cell.contentView addSubview:districtLable];
    return cell;
}

#pragma mark ------------------------------------------------------------
#pragma mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _distructLable.text = [NSString stringWithFormat:@"%@ %@", _cityMenu.itemsArray[_cityMenu.selectedIndex], _districtNames[indexPath.item]];
    _selectedIndex = indexPath.item;
    [collectionView reloadData];
}

#pragma mark ------------------------------------------------------------
#pragma mark RLPullDownMenuDelegate
- (void)pullDownMenu:(RLPullDownMenu *)pullDownMenu didSelectRow:(NSInteger)row {
    _selectedIndex = INT_MAX;
    switch (row) {
        case 0:
            _districtNames = beijin;
            [_districtCollectionView reloadData];
            break;
        case 1:
            _districtNames = shanghai;
            [_districtCollectionView reloadData];
            break;
        case 2:
            _districtNames = guangzhou;
            [_districtCollectionView reloadData];
            break;
        case 3:
            _districtNames = hangzhou;
            [_districtCollectionView reloadData];
            break;
            
        default:
            break;
    }
}

@end
